package tool;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 编写部分公共类，放置对象
 */
public class OtherToolClass {

    //实例化对象
    public BaseDao baseDao=new BaseDao();
    Scanner input=new Scanner(System.in);
    /**
     * 获取本地时间
     * @return
     */
    public String getTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = dateFormat.format(new Date());
        return date;
    }

    /**获取自增ID,有年月日
     * @param sql
     * @param number
     * @return
     */
    public String getID(String sql, String number,RowMapper row) throws SQLException, ClassNotFoundException {
        String id=number;
        Object object= baseDao.excuteQueryOne(sql,null, row);
        if (object==null){
            id=id+getTime()+"0001";
        }else {
            id=object.toString();
            String x=id.substring(3,11);
            if (x.equals(getTime())){
                int y = Integer.parseInt(id.substring(11))+1;
                if (y < 10) {
                    id = number + getTime() + "000" + y;
                } else if (y >= 10 && y < 100) {
                    id = number + getTime() + "00" + y;
                } else if (y >= 100 && y < 1000) {
                    id = number + getTime() + "0" + y;
                }else if (y >= 100) {
                    id = number + getTime() + y;
                }
            }else {
                id=number+getTime()+"0001";
            }
        }
        return id;
    }
    //获取自增id，无年月日
    public String getIDtwo(String sql,String number,RowMapper row) throws SQLException, ClassNotFoundException {
        String id=number;
        Object object= baseDao.excuteQueryOne(sql,null,row);
        if (object==null){
            id=id+"0001";
        }else {
            int y = Integer.parseInt(id.substring(3))+1;
            if (y < 10) {
                id = number + "000" + y;
            } else if (y >= 10 && y < 100) {
                id = number + "00" + y;
            } else if (y >= 100 && y < 1000) {
                id = number + "0" + y;
            }else if (y >= 100) {
                id = number + y;
            }
        }
        return id;
    }

    public boolean onceString(String str,String cha){
        boolean a=false;
        //入果字符穿为空，返回false
        if (null==str||"".equals (str)){
            return false;
        }
        //定义字符和出现次数的map集合
        Map<Character,Integer> count=new HashMap <Character, Integer> ();
        //将数组中的字符存入数组
        char[] temp=str.toCharArray ();
        //遍历字符，将次数计入map
        for (char ch: temp) {
            if (count.containsKey (ch)){
                Integer value=count.get (ch);
                count.put (ch,++value);
            }else {
                count.put (ch,1);
            }
        }
        //遍历字符，区第一个值
        for (char key : temp) {
            if (count.get (key).equals (cha)){
                a=true;
            }
        }
        return a;
    }
}